{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "from my_py_toolkit.file.excel.excel_toolkit import write_excel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "p = '../outputs/bert-resources_bert_model_bert/run.log'\n",
    "titles = []\n",
    "values = []\n",
    "reg_replace = '.*- __main__ - '\n",
    "reg_content = 'Dev\\\\n-{80}\\\\n(?P<content>[\\\\s\\\\S]*?)\\\\n-{80}\\\\n\\*\\*'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "78646"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = ''\n",
    "with open(p, 'r', encoding='utf-8') as f:\n",
    "    data = f.read()\n",
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "46486"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data = re.sub(reg_replace, '', data)\n",
    "len(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(['f1', 'precision', 'recall'],\n",
       " [['4.464285714283722e-13', '1.0', '2.232142857142359e-13'],\n",
       "  ['4.464285714283722e-13', '1.0', '2.232142857142359e-13'],\n",
       "  ['0.16260162601660058', '0.9090909090911157', '0.08928571428591758'],\n",
       "  ['0.47968545216265274', '0.580952380952514', '0.40848214285727485'],\n",
       "  ['0.522388059701635', '0.7882882882883836', '0.390625000000136'],\n",
       "  ['0.6307884856071012', '0.7179487179487983', '0.5625000000000976'],\n",
       "  ['0.6923950056754388', '0.7043879907621929', '0.6808035714286427'],\n",
       "  ['0.695852534562282', '0.7190476190476859', '0.6741071428572156'],\n",
       "  ['0.7030716723550163', '0.7169373549884648', '0.6897321428572121'],\n",
       "  ['0.6682188591386105', '0.6982968369830418', '0.6406250000000802'],\n",
       "  ['0.7246376811594868', '0.7894736842105817', '0.6696428571429309'],\n",
       "  ['0.7325581395349459', '0.7645631067961737', '0.7031250000000663'],\n",
       "  ['0.7165259348613471', '0.779527559055176', '0.6629464285715038'],\n",
       "  ['0.6944785276074369', '0.7711171662125964', '0.6316964285715108'],\n",
       "  ['0.7188208616780682', '0.7304147465438409', '0.707589285714351'],\n",
       "  ['0.7296037296037926', '0.7634146341463992', '0.6986607142857816'],\n",
       "  ['0.6816479400749859', '0.7733711048159282', '0.6093750000000872'],\n",
       "  ['0.7259953161593148', '0.7635467980296149', '0.6919642857143544'],\n",
       "  ['0.6995305164319954', '0.7376237623763026', '0.6651785714286461'],\n",
       "  ['0.732203389830569', '0.7414187643021186', '0.7232142857143475'],\n",
       "  ['0.7256027554535647', '0.747044917257743', '0.7053571428572086'],\n",
       "  ['0.6666666666667502', '0.7600000000000685', '0.5937500000000907'],\n",
       "  ['0.6709844559586344', '0.7993827160494447', '0.5781250000000941'],\n",
       "  ['0.7245714285714915', '0.7423887587822617', '0.707589285714351'],\n",
       "  ['0.7388235294118262', '0.7810945273632385', '0.7008928571429239'],\n",
       "  ['0.7341772151899346', '0.7577197149644281', '0.7120535714286357'],\n",
       "  ['0.750000000000059', '0.7950000000000512', '0.7098214285714933'],\n",
       "  ['0.7442396313364644', '0.769047619047674', '0.7209821428572051'],\n",
       "  ['0.5730027548210542', '0.7482014388490115', '0.4642857142858339'],\n",
       "  ['0.7380675203725872', '0.7712895377129511', '0.707589285714351'],\n",
       "  ['0.7395348837209907', '0.7718446601942301', '0.7098214285714933'],\n",
       "  ['0.7468785471056193', '0.7598152424942818', '0.7343750000000593'],\n",
       "  ['0.7453271028037978', '0.7818627450980927', '0.7120535714286357'],\n",
       "  ['0.7374562427071791', '0.772616136919371', '0.7053571428572086'],\n",
       "  ['0.7433217189315346', '0.7748184019371005', '0.714285714285778'],\n",
       "  ['0.7389277389277997', '0.7731707317073724', '0.707589285714351'],\n",
       "  ['0.7439724454650416', '0.7659574468085659', '0.7232142857143475'],\n",
       "  ['0.741676234213607', '0.763593380614713', '0.7209821428572051'],\n",
       "  ['0.741676234213607', '0.763593380614713', '0.7209821428572051'],\n",
       "  ['0.741676234213607', '0.763593380614713', '0.7209821428572051']])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for match in re.finditer(reg_content, data):\n",
    "    cur_titles = []\n",
    "    cur_values = []\n",
    "    for line in match.group('content').split('\\n'):\n",
    "        k,v = [s.strip() for s in line.split('=')]\n",
    "        cur_titles.append(k)\n",
    "        cur_values.append(v)\n",
    "    titles = cur_titles if not titles else titles\n",
    "    values.append(cur_values)\n",
    "titles, values    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def handle_metric(data_path, save_path):\n",
    "    titles = []\n",
    "    values = []\n",
    "    reg_replace = '.*- __main__ - '\n",
    "    reg_content = 'Dev\\\\n-{80}\\\\n(?P<content>[\\\\s\\\\S]*?)\\\\n-{80}\\\\n\\*\\*'\n",
    "    \n",
    "    data = ''\n",
    "    with open(p, 'r', encoding='utf-8') as f:\n",
    "        data = f.read()\n",
    "    \n",
    "    data = re.sub(reg_replace, '', data)\n",
    "    for match in re.finditer(reg_content, data):\n",
    "        cur_titles = []\n",
    "        cur_values = []\n",
    "        for line in match.group('content').split('\\n'):\n",
    "            k,v = [s.strip() for s in line.split('=')]\n",
    "            cur_titles.append(k)\n",
    "            cur_values.append(v)\n",
    "        titles = cur_titles if not titles else titles\n",
    "        values.append(cur_values)\n",
    "    titles, values\n",
    "    write_excel([titles] + values, save_path)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "handle_metric(p, './handle_metrics.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['7600', '0.00000167', '-3.09702210']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "line =  '04/10/2022 09:23:30 - INFO - __main__ - global_steps 20 - lr: 0.00000075  loss: 255.85011978'\n",
    "line = '04/10/2022 10:47:10 - INFO - __main__ - global_steps 7600 - lr: 0.00000167  loss: -3.09702210'\n",
    "reg = 'global_steps (?P<global_steps>\\d+) - lr: (?P<lr>\\d+\\.\\d+)  loss: (?P<loss>(-)?\\d+\\.\\d+)'\n",
    "print(list(re.search(reg, line).groupdict().values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 处理 loss\n",
    "# 04/10/2022 09:23:30 - INFO - __main__ - global_steps 20 - lr: 0.00000075  loss: 255.85011978\n",
    "line =  '04/10/2022 09:23:30 - INFO - __main__ - global_steps 20 - lr: 0.00000075  loss: 255.85011978'\n",
    "def handle_loss(data_path, save_path, epochs=20):\n",
    "    info = {}\n",
    "    reg = 'global_steps (?P<global_steps>\\d+) - lr: (?P<lr>\\d+\\.\\d+)  loss: (?P<loss>(-)?\\d+\\.\\d+)'\n",
    "    with open(data_path, 'r', encoding='utf-8') as f:\n",
    "        for line in f.read().split('\\n'):\n",
    "            match = re.search(reg, line)\n",
    "            if match:\n",
    "                for k,v in match.groupdict().items():\n",
    "                    if k not in info:\n",
    "                        info[k] = []\n",
    "                    info[k].append(float(v))\n",
    "    res = [['min', 'max', 'avg']]\n",
    "    avg_lens = len(info['loss']) // epochs\n",
    "    for i in range(epochs):\n",
    "        loss = info['loss'][i*avg_lens:(i+1)*avg_lens]\n",
    "        res.append([min(loss), max(loss), sum(loss)/len(loss)])\n",
    "    write_excel(res, save_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'loss'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-4b482a7f4be3>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mp\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'../outputs/bert-resources_bert_model_bert/run.log'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mhandle_loss\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'./handle_loss.xlsx'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-19-d8ded67ac454>\u001b[0m in \u001b[0;36mhandle_loss\u001b[1;34m(data_path, save_path, epochs)\u001b[0m\n\u001b[0;32m     14\u001b[0m                     \u001b[0minfo\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mk\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m     \u001b[0mres\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'min'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'max'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'avg'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m     \u001b[0mavg_lens\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minfo\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'loss'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m//\u001b[0m \u001b[0mepochs\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mepochs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     18\u001b[0m         \u001b[0mloss\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minfo\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'loss'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mavg_lens\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m+\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mavg_lens\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyError\u001b[0m: 'loss'"
     ]
    }
   ],
   "source": [
    "p = '../outputs/bert-resources_bert_model_bert/run.log'\n",
    "handle_loss(p, './handle_loss.xlsx')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1021"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "with open(p, 'r', encoding='utf-8') as f:\n",
    "    lines = f.read().split('\\n')\n",
    "len(lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['04/10/2022 10:49:34 - INFO - utils.utils - remove old ckpt: outputs\\\\bert-resources_bert_model_bert\\\\ckpt\\\\step-8000-spo-f1-0.741676234213607',\n",
       " '04/10/2022 10:49:34 - INFO - __main__ - *************************************',\n",
       " '']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lines[-3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "71"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len([ line for line in lines if re.search(reg, line) ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "9ceb4fbcb160ec6874bdeb6dd3ea466ee8a92d5e1e96ae5f52eaf4521fd48069"
  },
  "kernelspec": {
   "display_name": "Python 3.6.5 ('gp_torch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
